package com.example.epic.controller;

import com.example.epic.bean.Application;
import com.example.epic.common.ResponseData;
import com.example.epic.service.ApplicationService;
import com.example.epic.service.ControlLevelService;
import com.example.epic.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/wx")
public class ApplicationController {

    @Autowired
    ApplicationService applicationService;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    ControlLevelService controlLevelService;

    /**
     * 学生提交申请
     * @param application 申请表单
     * @return
     */
    @PostMapping(value = "/applications")
    @ResponseBody
    public ResponseData submitApplication(Application application){
        application.setTeacherId("t201620010000");
        application.setStudentId("201620010027");
        System.out.println("application = " + application);
        // 查询防控等级
        Integer levelStatus = (Integer) redisUtils.get("controlLevel");
        if(levelStatus == null){
            levelStatus = controlLevelService.getControlLevel();
            redisUtils.set("controlLevel", levelStatus);
        }

        if(levelStatus == 3){
            return ResponseData.fail("当前处于严格防控状态，不允许外出");
        }
        else if(levelStatus == 1){
            return ResponseData.fail("目前进出不需要申请，可直接扫码外出");
        }

        applicationService.saveApplication(application);
        return ResponseData.success("提交申请成功");
    }

    /**
     * 通过用户id分辨是学生还是老师。把与之相关的申请查询出来并返回
     * @param id 用户id，可能是学生，可能是老师
     * @return
     */
    @GetMapping(value = "/applications")
    @ResponseBody
    public ResponseData findAllApplicationById(@RequestParam("id") String id){
        System.out.println("id = " + id);
        List<Application> applications = null;
        if(id.charAt(0) == 't')
            applications = applicationService.findApplicationByTeacherId(id);
        else
            applications = applicationService.findApplicationByStudentId(id);

        System.out.println("applications = " + applications);
        return ResponseData.success("申请表清单", applications);
    }


    /**
     * 老师同意某一个申请，即把表单的同意字段设为1。先判断终止时间是否已经过了，然后将终止时间和现在的时间相减作为redis的失效时间，键是Sid&Aid，值是申请。
     * @param application 申请的表单
     * @return
     */
    @PutMapping(value = "/applications")
    @ResponseBody
    public ResponseData agreeApplication(@RequestBody Application application){
        System.out.println("application = " + application);

        Date now = new Date();

        long expireTime = (application.getEndTime().getTime() - now.getTime()) / 1000;
        System.out.println("expireTime = " + expireTime);

        if(expireTime <= 0)
            return ResponseData.fail("操作失败，已经过了所申请的终止时间");

        applicationService.agreeApplication(application.getId());
        redisUtils.set(application.getStudentId()+"&"+application.getId(),application,expireTime);
        return ResponseData.success("同意申请");
    }
}
